/*
*   @Author: 1296
*   @Date: 2025-03-22
*   @Description: Vuex Store 配置
*/
import { createStore } from 'vuex'
import defaultAvatar from '@/assets/guest.jpg'

function parseSessionStorage(key) {
    const value = sessionStorage.getItem(key);
    return value === 'null' ? null : value;
}

export default createStore({
    state: {
        user: {
            id: parseSessionStorage('id'),
            token: parseSessionStorage('userToken'),
            username: parseSessionStorage('username'),
            avatar: parseSessionStorage('avatar') || defaultAvatar,
            phone: parseSessionStorage('phone'),
            gender: parseSessionStorage('gender'),
            email: parseSessionStorage('email'),
            institute: parseSessionStorage('institute'),
            major: parseSessionStorage('major'),
            className: parseSessionStorage('className'),
            role: parseSessionStorage('role'),
            jobCode: parseSessionStorage('jobCode')
        },
        baseURL: 'http://192.168.121.237:8080'
    },
    mutations: {
        SET_USER(state, payload) {
            state.user = {
                id: payload.id,
                token: payload.token,
                username: payload.username,
                avatar: payload.avatar || defaultAvatar,
                phone: payload.phone,
                gender: payload.gender,
                email: payload.email,
                institute: payload.institute,
                major: payload.major,
                className: payload.className,
                role: payload.role,
                jobCode: payload.jobCode
            };

            // 统一存储逻辑：非空值存储，否则移除
            const setOrRemove = (key, value) => {
                if (value != null) {
                    sessionStorage.setItem(key, value);
                } else {
                    sessionStorage.removeItem(key);
                }
            };

            setOrRemove('id', payload.id);
            setOrRemove('userToken', payload.token);
            setOrRemove('username', payload.username);
            setOrRemove('avatar', payload.avatar);
            setOrRemove('phone', payload.phone);
            setOrRemove('gender', payload.gender);
            setOrRemove('email', payload.email);
            setOrRemove('institute', payload.institute);
            setOrRemove('major', payload.major);
            setOrRemove('className', payload.className);
            setOrRemove('role', payload.role);
            setOrRemove('jobCode', payload.jobCode);
        },
        CLEAR_USER(state) {
            state.user = {
                id: null,
                token: null,
                username: null,
                avatar: defaultAvatar,
                phone: null,
                gender: null,
                email: null,
                institute: null,
                major: null,
                className: null,
                role: null,
                jobCode: null
            };
            // 清空所有相关键
            sessionStorage.removeItem('id');
            sessionStorage.removeItem('userToken');
            sessionStorage.removeItem('username');
            sessionStorage.removeItem('avatar');
            sessionStorage.removeItem('phone');
            sessionStorage.removeItem('gender');
            sessionStorage.removeItem('email');
            sessionStorage.removeItem('institute');
            sessionStorage.removeItem('major');
            sessionStorage.removeItem('className');
            sessionStorage.removeItem('role');
            sessionStorage.removeItem('jobCode')
        },
        SET_BASE_URL(state, payload) {
            state.baseURL = payload;
        }
    },
    getters: {
        id: (state) => state.user.id,
        token: (state) => state.user.token,
        isLoggedIn: (state) => !!state.user.token,
        username: (state) => state.user.username,
        avatar: (state) => state.user.avatar,
        email: (state) => state.user.email,
        role: (state) => state.user.role,
        phone: (state) => state.user.phone,
        gender: (state) => state.user.gender,
        institute: (state) => state.user.institute,
        major: (state) => state.user.major,
        className: (state) => state.user.className,
        jobCode: (state) => state.user.jobCode,
        getBaseURL: (state) => state.baseURL
    }
});